package com.xiwi.vip.ziti.common.core; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit;
@Component public class RedisUtil { @Resource private RedisTemplate<String, Object> redisTemplate; private String tag = ""; public RedisUtil tag(String tag) { this.tag = tag; return this; } public Set<String> keys(String keys){ try { keys = tag + keys; Set<String> result = redisTemplate.keys(keys); tag = ""; return result; }catch (Exception e){ e.printStackTrace(); return null; } }
public boolean expire(String key, long time) { try { key = tag + key; if (time > 0) { redisTemplate.expire(key, time, TimeUnit.SECONDS); } tag = ""; return true; } catch (Exception e) { e.printStackTrace(); return false; } }
public long getExpire(String key) { key = tag + key; long result = redisTemplate.getExpire(key, TimeUnit.SECONDS); tag = ""; return result; }
public boolean hasKey(String key) { try { key = tag + key; boolean result = redisTemplate.hasKey(key); tag = ""; return result; } catch (Exception e) { e.printStackTrace(); return false; } }
@SuppressWarnings("unchecked") public void del(String... key) { Arrays.asList(key).forEach(item -> item=tag+item); if (key != null && key.length > 0) { if (key.length == 1) { redisTemplate.delete(key[0]); } else { redisTemplate.delete(Arrays.asList(key)); } } tag = ""; }
public Object get(String key) { key = tag + key; Object result = key == null ? null : redisTemplate.opsForValue().get(key); tag = ""; return result; }
public <T> T get(String key, Class<T> clazz) { key = tag + key; T result = key == null ? null : clazz.cast(redisTemplate.opsForValue().get(key)); tag = ""; return result; }
public boolean set(String key, Object value) { try { key = tag + key; redisTemplate.opsForValue().set(key, value); tag = ""; return true; } catch (Exception e) { e.printStackTrace(); return false; } }
public boolean set(String key, Object value, long time) { try { key = tag + key; if (time > 0) { redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); } else { set(key, value); } tag = ""; return true; } catch (Exception e) { e.printStackTrace(); return false; } }
public long incr(String key, long delta) { if (delta < 0) { throw new RuntimeException("递增因子必须大于0"); } key = tag + key; Long result = redisTemplate.opsForValue().increment(key, delta); tag = ""; return result; }
public long decr(String key, long delta) { if (delta < 0) { throw new RuntimeException("递减因子必须大于0"); } key = tag + key; Long result = redisTemplate.opsForValue().increment(key, -delta); tag = ""; return result; }
public Object hget(String key, String item) { key = tag + key; Object result = redisTemplate.opsForHash().get(key, item); tag = ""; return result; }
public Map<Object, Object> hmget(String key) { key = tag + key; Map<Object, Object> result = redisTemplate.opsForHash().entries(key); tag = ""; return result; }
public boolean hmset(String key, Map<String, Object> map) { try { key = tag + key; redisTemplate.opsForHash().putAll(key, map); tag = ""; return true; } catch (Exception e) { e.printStackTrace(); return false; } }
public boolean hmset(String key, Map<String, Object> map, long time) { try { key = tag + key; redisTemplate.opsForHash().putAll(key, map); if (time > 0) { expire(key, time); } tag = ""; return true; } catch (Exception e) { e.printStackTrace(); return false; } }
public boolean hset(String key, String item, Object value) { try { key = tag + key; redisTemplate.opsForHash().put(key, item, value); tag = ""; return true; } catch (Exception e) { e.printStackTrace(); return false; } }
public boolean hset(String key, String item, Object value, long time) { try { key = tag + key; redisTemplate.opsForHash().put(key, item, value); if (time > 0) { expire(key, time); } tag = ""; return true; } catch (Exception e) { e.printStackTrace(); return false; } }
public void hdel(String key, Object... item) { key = tag + key; redisTemplate.opsForHash().delete(key, item); tag = ""; }
public boolean hHasKey(String key, String item) { key = tag + key; boolean result = redisTemplate.opsForHash().hasKey(key, item); tag = ""; return result; }
public double hincr(String key, String item, double by) { key = tag + key; double result = redisTemplate.opsForHash().increment(key, item, by); tag = ""; return result; }
public double hdecr(String key, String item, double by) { key = tag + key; double result = redisTemplate.opsForHash().increment(key, item, -by); tag = ""; return result; }
public Set<Object> sGet(String key) { try { key = tag + key; Set<Object> result = redisTemplate.opsForSet().members(key); tag = ""; return result; } catch (Exception e) { e.printStackTrace(); return null; } }
public boolean sHasKey(String key, Object value) { try { key = tag + key; boolean result = redisTemplate.opsForSet().isMember(key, value); tag = ""; return result; } catch (Exception e) { e.printStackTrace(); return false; } }
public long sSet(String key, Object... values) { try { key = tag + key; long result = redisTemplate.opsForSet().add(key, values); tag = ""; return result; } catch (Exception e) { e.printStackTrace(); return 0; } }
public long sSetAndTime(String key, long time, Object... values) { try { key = tag + key; Long count = redisTemplate.opsForSet().add(key, values); if (time > 0) expire(key, time); tag = ""; return count; } catch (Exception e) { e.printStackTrace(); return 0; } }
public long sGetSetSize(String key) { try { key = tag + key; long result = redisTemplate.opsForSet().size(key); tag = ""; return result; } catch (Exception e) { e.printStackTrace(); return 0; } }
public long setRemove(String key, Object... values) { try { key = tag + key; Long count = redisTemplate.opsForSet().remove(key, values); tag = ""; return count; } catch (Exception e) { e.printStackTrace(); return 0; } }
public List<Object> lGet(String key, long start, long end) { try { key = tag + key; List<Object> result = redisTemplate.opsForList().range(key, start, end); tag = ""; return result; } catch (Exception e) { e.printStackTrace(); return null; } }
public long lGetListSize(String key) { try { key = tag + key; long result = redisTemplate.opsForList().size(key); tag = ""; return result; } catch (Exception e) { e.printStackTrace(); return 0; } }
public Object lGetIndex(String key, long index) { try { key = tag + key; Object result = redisTemplate.opsForList().index(key, index); tag = ""; return result; } catch (Exception e) { e.printStackTrace(); return null; } }
public boolean lSet(String key, Object value) { try { key = tag + key; redisTemplate.opsForList().rightPush(key, value); tag = ""; return true; } catch (Exception e) { e.printStackTrace(); return false; } }
public boolean lSet(String key, Object value, long time) { try { key = tag + key; redisTemplate.opsForList().rightPush(key, value); if (time > 0) expire(key, time); tag = ""; return true; } catch (Exception e) { e.printStackTrace(); return false; } }
public boolean lSet(String key, List<Object> value) { try { key = tag + key; redisTemplate.opsForList().rightPushAll(key, value); tag = ""; return true; } catch (Exception e) { e.printStackTrace(); return false; } }
public boolean lSet(String key, List<Object> value, long time) { try { key = tag + key; redisTemplate.opsForList().rightPushAll(key, value); if (time > 0) expire(key, time); tag = ""; return true; } catch (Exception e) { e.printStackTrace(); return false; } }
public boolean lUpdateIndex(String key, long index, Object value) { try { key = tag + key; redisTemplate.opsForList().set(key, index, value); tag = ""; return true; } catch (Exception e) { e.printStackTrace(); return false; } }
public long lRemove(String key, long count, Object value) { try { key = tag + key; Long remove = redisTemplate.opsForList().remove(key, count, value); tag = ""; return remove; } catch (Exception e) { e.printStackTrace(); return 0; } } }
|